package com.fh.modules.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.query.QueryWrapperUtil;
import com.fh.modules.pms.entity.PmsProductCategory;
import com.fh.modules.pms.model.mobile.ProductCategoryModel;
import com.fh.modules.pms.service.PmsProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @Title: Controller
 * @Description: 商品分类
 * @author： code generator
 * @date： 2019-05-13
 * @version： V1.0
 */
@RestController
@Slf4j
@Api("商品分类")
public class PmsProductCategoryController {
    @Autowired
    private PmsProductCategoryService pmsProductCategoryService;

    /**
     *  移动端查询所有商品分类
     *
     * @return
     */
    @GetMapping(value = "/pms/pmsProductCategory/queryAll")
    @ApiOperation("移动端查询所有商品分类")
    @AutoLog(value = "移动端查询所有商品分类", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryAll() {
        return Result.ok(pmsProductCategoryService.queryAll());
    }

    /**
     * 分页列表查询
     *
     * @param pmsProductCategory
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/pms/pmsProductCategory/list")
    @ApiOperation("分页查询商品分类")
    public Result<IPage<PmsProductCategory>> queryPageList(PmsProductCategory pmsProductCategory,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                           HttpServletRequest req) {
        Result<IPage<PmsProductCategory>> result = new Result<IPage<PmsProductCategory>>();
        QueryWrapper<PmsProductCategory> queryWrapper = new QueryWrapper<PmsProductCategory>();
        QueryWrapperUtil.initQueryWrapper(queryWrapper, pmsProductCategory);
        Page<PmsProductCategory> page = new Page<PmsProductCategory>(pageNo, pageSize);
        IPage<PmsProductCategory> pageList = pmsProductCategoryService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加
     *
     * @param pmsProductCategory
     * @return
     */
    @PostMapping(value = "/pms/pmsProductCategory/add")
    @AutoLog("增加商品类型记录")
    public Result<PmsProductCategory> add(@RequestBody PmsProductCategory pmsProductCategory) {
        Result<PmsProductCategory> result = new Result<PmsProductCategory>();
        try {
            pmsProductCategoryService.save(pmsProductCategory);
            result.success("添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 编辑
     *
     * @param pmsProductCategory
     * @return
     */
    @PutMapping(value = "/pms/pmsProductCategory/edit")
    public Result<PmsProductCategory> edit(@RequestBody PmsProductCategory pmsProductCategory) {
        Result<PmsProductCategory> result = new Result<PmsProductCategory>();
        PmsProductCategory pmsProductCategoryEntity = pmsProductCategoryService.getById(pmsProductCategory.getId());
        if (pmsProductCategoryEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = pmsProductCategoryService.updateById(pmsProductCategory);
            if (ok) {
                result.success("修改成功!");
            }
        }
        return result;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/pms/pmsProductCategory/delete")
    public Result<PmsProductCategory> delete(@RequestParam(name = "id", required = true) String id) {
        Result<PmsProductCategory> result = new Result<PmsProductCategory>();
        PmsProductCategory pmsProductCategory = pmsProductCategoryService.getById(id);
        if (pmsProductCategory == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = pmsProductCategoryService.removeById(id);
            if (ok) {
                result.success("删除成功!");
            }
        }
        return result;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/pms/pmsProductCategory/deleteBatch")
    public Result<PmsProductCategory> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        Result<PmsProductCategory> result = new Result<PmsProductCategory>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            this.pmsProductCategoryService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/pms/pmsProductCategory/queryById")
    public Result<PmsProductCategory> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<PmsProductCategory> result = new Result<PmsProductCategory>();
        PmsProductCategory pmsProductCategory = pmsProductCategoryService.getById(id);
        if (pmsProductCategory == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(pmsProductCategory);
            result.setSuccess(true);
        }
        return result;
    }

}
